/**
 * Created by seawind8888 on 16/5/18.
 */
function format_number(n) {
    var b = parseInt(n).toString();
    var len = b.length;
    if (len <= 3) {
        return b;
    }
    var r = len % 3;
    return r > 0 ? b.slice(0, r) + "," + b.slice(r, len).match(/\d{3}/g).join(",") : b.slice(r, len).match(/\d{3}/g).join(",");
}


$(function () {
    var userId = GetQueryString("user_id");
    $("#_userId").val(userId);
	// 初始化用户积分信息
	initUserPointPrize();
	
    // 抽奖操作
    $('#pointer').click(function () {
    	takePrize();
    });
    $('#_zcyc').click(function () {
    	takePrize();
    });
    
    $('#_not_zcyc').click(function () {
    	takePrize();
    });
    
    var bRotate = false;
    // 抽奖操作
    function takePrize(){
    	if (!bRotate){
	    	bRotate = true;
	            
	    	$.getJSON("../pointc/saveLuckDraw?jsoncallback=?&userId="+userId,function(json){
	            if(json.status != '200'){
	        		alert(json.msg);
	            	return;
	            }
	            // 奖品类型  1：实物  0：虚拟产品 2：积分
	            var type = json.prizeMap.type;
	            $("#_uphId").val(json.prizeMap.uphId);
	
	        	var rotateFn = function (awards, angles, prizeName) {
	                $('#rotate').stopRotate();
	                $('#rotate').rotate({
	                    angle: 0,
	                    animateTo: angles + 1800,
	                    duration: 8000,
	                    callback: function () {
	                    	$("#_shareTitle").val("我在诚信贷积分商城抽中了"+prizeName+"，你猜我抽了几次？");
	                    	if(type == "3"){
	                   		 	$(".losing_lottery,.model").fadeIn();
	                    	}else{
	                           $(".raffle_success,.model").fadeIn();
	                           $("#gifts_name").html("恭喜您抽中" + prizeName);
	                           $("#gifts_info").hide();
	                           $("#gifts_info_two").hide();
	                           
	                           // 实物奖品
	                           if(type=="1"){
	                           		$("#_qrsh").show();
	                           		$("#gifts_info").html(json.prizeMap.prizeMsg).show();
	                           		$("#gifts_info_two").show();
	                           }
	                           
	                           // 虚拟奖品
	                           else if(type=="0"){
	                               $("#gifts_info").html(json.prizeMap.prizeMsg).show();
	                               $("#_qrsh").hide();
	                           }
	                           // 积分奖品
	                           else if(type=="2"){
	                               $("#_qrsh").hide();
	                           }
	                    	}
	                           
	                    	initUserPointPrize();
	                    	bRotate = false;
	                    }
	                });
	           	};
	            switch (1) {
	                case 1:
	                	rotateFn(1, json.prizeMap.position, json.prizeMap.name);
	                    break;
	            }
	        });
    	}
	}
    
    
    $(".again").click(function(){
        $(".not_enough_tips,.raffle_success,.losing_lottery,.model").hide();
        $('#pointer').click();
    });

    var ua = navigator.userAgent;
    if(ua.match(/(Android)/i)){
        $(".raffle_rotate").css("top","13.5%");
        $(".info").css("top","22em");
        if (ua.match(/(SAMSUNG)/i)) {
            $(".raffle_rotate").css("top","11.5%");
            $(".info").css("padding-top","21em");
            $(".tips_info>a").css("margin-top",".1em");
            $(".return_share>ul>li img").css("width","45%");
            $(".return_share>ul>li").css("padding","0");
        }
    }
});

function not_cancel() {
    $(".not_enough_tips,.raffle_success,.losing_lottery,.model").fadeOut();
}

function confirm(){
    location.href = "http://confirm?uphId="+$("#_uphId").val();
}
function gain(){
    location.href = "http://gain";
}

function share_weibo(){
    location.href = "http://share?platform=weibo&title="+$("#_shareTitle").val();
}
function share_moment(){
    location.href = "http://share?platform=moment&title="+$("#_shareTitle").val();
}
function share_qzone(){
    location.href = "http://share?platform=qzone&title="+$("#_shareTitle").val();
}
function more(){
    location.href = "http://more";
}

//初始化用户积分与抽奖次数
function initUserPointPrize() {
	$.getJSON("../pointc/user_point_prize?jsoncallback=?&userId="+$("#_userId").val(),function(json){
		$("#integral_total").html(json.integral_total);
		$("#can_prize").html(json.can_prize);
	});
}

/**
 * 获取参数
 */
function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return unescape(r[2]);
	return null;
}